﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Windows;
using Microsoft.Win32;

using GWApplication;

using FXnaInfrastructure;


namespace GWPresentation
{


    public sealed class ApplicationService
    {

        protected ApplicationService()
        {
 
        }


        static ApplicationService currentService;


        /// <summary>
        /// 提供单件封装
        /// </summary>
        public static ApplicationService CurrentService
        {
            get {

                if (currentService==null)
                {
                    currentService=new ApplicationService ();
                    
                }
                

                return ApplicationService.currentService; }
           
        }



        #region 关键应用流程控制

         /// <summary>
        /// 初始化所有组件系统，整个应用程序只需要调用一次
        /// </summary>
        public void InitModules()
        {




            //所有需要监测project的，进行初始订阅

            AppViewModel.Current.Init();
            GisService.CurrentService.Init();






        }


        public void DisposeModules()
        {

 

        }

        #endregion


        #region 公共服务部分

        /// <summary>
        /// 取得应用程序下的特定目录
        /// </summary>
        /// <param name="pathType"></param>
        /// <returns></returns>
        public string GetApplicationPath(ApplicationPath pathType)
        {
            string path = "";

            switch (pathType)
            {
                case ApplicationPath.MODELEXEPATH:

                    path = AppDomain.CurrentDomain.BaseDirectory + "bin\\";



                    break;
                case ApplicationPath.APPPATH:

                    path = AppDomain.CurrentDomain.BaseDirectory;

                    break;
                default:
                    break;
            }

            return path;




        }




        ///下面是项目操作相关的接口


        /// <summary>
        /// 打开一个项目
        /// </summary>
        public void OpenProject()
        {
            System.Windows.MessageBoxResult mbr = this.IsSaveBeforeClose();
            if (mbr == MessageBoxResult.Cancel)
            {
                return;
            }


            OpenFileDialog openfd = new OpenFileDialog();
            openfd.Filter = ".gw文件（*.gw）|*.gw";
            if (openfd.ShowDialog() == true)
            {



                ProjectService.CurrentService.Open(@openfd.FileName);



                //程序配置操作

                if (!ProjectService.CurrentService.BNullProj)
                {



                    History h = new History(DateTime.Now, ProjectService.CurrentService.GetProjMainFile(), ProjectService.CurrentService.GetProjName());

                    ConfigService.CurrentService.SettingInfo.AddHistory(h);



                }



            }

        }


        public void OpenProject(string filePath)
        {

            System.Windows.MessageBoxResult mbr = this.IsSaveBeforeClose();
            if (mbr == MessageBoxResult.Cancel)
            {
                return;
            }

            ProjectService.CurrentService.Open(filePath);



            //程序配置操作

            if (!ProjectService.CurrentService.BNullProj)
            {



                History h = new History(DateTime.Now, ProjectService.CurrentService.GetProjMainFile(), ProjectService.CurrentService.GetProjName());

                ConfigService.CurrentService.SettingInfo.AddHistory(h);



            }


        }

        /// <summary>
        /// 保存一个项目
        /// </summary>
        public void SaveProject()
        {
            if (!ProjectService.CurrentService.BNullProj)
            {
                ProjectService.CurrentService.Save();

            }


        }

        /// <summary>
        /// 关闭一个项目
        /// </summary>
        public void CloseProject()
        {
            this.IsSaveBeforeClose();

        }


        /// <summary>
        /// 关闭前是否保存
        /// </summary>
        protected MessageBoxResult IsSaveBeforeClose()
        {

            //首先判断当前项目是否为空

            if (!ProjectService.CurrentService.BNullProj)
            {
                System.Windows.MessageBoxResult mbr = System.Windows.MessageBox.Show("是否当前保存？", "提示", System.Windows.MessageBoxButton.YesNoCancel);

                //Console.WriteLine("返回值是：" + mbr.ToString());

                if (mbr == MessageBoxResult.Yes)
                {
                    ProjectService.CurrentService.Save();

                    ProjectService.CurrentService.CloseProj();
                }
                else if (mbr == MessageBoxResult.No)
                {
                    ProjectService.CurrentService.CloseProj();
                }
                else if (mbr == MessageBoxResult.Cancel)
                    return mbr;


                return mbr;

            }

            return MessageBoxResult.OK;


        }

        /// <summary>
        /// 应用程序创建新项目
        /// </summary>
        public void NewProject()
        {
            System.Windows.MessageBoxResult mbr = this.IsSaveBeforeClose();
            if (mbr == MessageBoxResult.Cancel)
            {
                return;
            }

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = ".gw文件（*.gw）|*.gw";
            if (sfd.ShowDialog() == true)
            {
                ProjectService.CurrentService.CreateNew(@sfd.FileName);

                ProjectService.CurrentService.Save();





                //程序配置操作

                if (!ProjectService.CurrentService.BNullProj)
                {



                    History h = new History(DateTime.Now, ProjectService.CurrentService.GetProjMainFile(), ProjectService.CurrentService.GetProjName());

                    ConfigService.CurrentService.SettingInfo.AddHistory(h);



                }





            }

        }

        #endregion










    }
}
